package com.talent.study.http;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;

import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.time.Duration;
import java.util.HashMap;
import java.util.stream.Collectors;

/**
 * @Author: hszhou
 * @Date: 2025/5/12 9:16
 * @Description: 必须描述类做什么事情, 实现什么功能
 */
@Slf4j
public class HttpClientTest {


    public static void main(String[] args) throws JsonProcessingException {


       String imageUrl = "https://webdata.cqccms.com.cn/webdata/ImagepassController/imagePass.do";

        HttpClient httpClient = HttpClient.newBuilder()
                .version(HttpClient.Version.HTTP_2)
                .connectTimeout(Duration.ofSeconds(10))
                .build();

      //  doGet(httpClient,  null, imageUrl);

        String url = "https://webdata.cqccms.com.cn/webdata/query/ZYCerti.do";
        HashMap<String,Object> params = new HashMap<>();
        params.put("pageNumber",1);
        params.put("pageSize",100);
        params.put("manuname","博兰得");
        params.put("imgPwd",7);

        doPostForm(httpClient, params, url);



    }

    private static void doPost(HttpClient httpClient,HashMap<String,String> params,String url) throws JsonProcessingException {
        ObjectMapper mapper = new ObjectMapper();
        String jsonBody = mapper.writeValueAsString(params);
        HttpRequest httpRequest = HttpRequest.newBuilder()
                .uri(URI.create(url))
                .header("Content-Type", "application/json")
                .POST(HttpRequest.BodyPublishers.ofString(jsonBody))
                .build();

        try {
            HttpResponse<String> response = httpClient.send(httpRequest, HttpResponse.BodyHandlers.ofString());
            System.out.println(response.statusCode());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    private static void doPostForm(HttpClient httpClient,HashMap<String,Object> params,String url){

        String formData = params.entrySet().stream()
                .map(entry -> entry.getKey() + "=" + entry.getValue())
                .collect(Collectors.joining("&"));
        // 创建请求
        HttpRequest httpRequest = HttpRequest.newBuilder()
                .uri(URI.create(url))
                .header("Content-Type", "application/x-www-form-urlencoded")
                .POST(HttpRequest.BodyPublishers.ofString(formData))
                .build();
        log.info("dd[{}]",httpRequest);
        try {
            HttpResponse<String> response = httpClient.send(httpRequest, HttpResponse.BodyHandlers.ofString());
            System.out.println(response.body());
            System.out.println(response.statusCode());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    private static void doGet(HttpClient httpClient,HashMap<String,String> params,String url) {



        HttpRequest httpRequest = HttpRequest.newBuilder()
                .uri(URI.create(url))
                .GET()
                .build();
        try {
            HttpResponse<String> response = httpClient.send(httpRequest, HttpResponse.BodyHandlers.ofString());
            System.out.println(response.statusCode());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
